package com.itheima.controller.impl;

import com.itheima.controller.lnter.IStudentCon;
import com.itheima.damain.Student;
import com.itheima.service.impl.StudentServicelmpl;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentConlmpl implements IStudentCon {

    private StudentServicelmpl studentServicelmpl = new StudentServicelmpl();

    private Scanner sc = new Scanner(System.in);

    @Override
    public void stiat() {
        //死循环
        loop:
        while (true) {
            // 键盘录入
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            String choice = sc.next();
            //根据不同的选择做不同的事
            switch (choice) {
                case "1":
                    //System.out.println("添加成功");
                    addStudent();
                    break;
                case "2":
                    //System.out.println("删除成功");
                    deleteStudent();
                    break;
                case "3":
                    // System.out.println("修改成功");
                    updateStudent();
                    break;
                case "4":
                    // System.out.println("查看成功");
                    findAllStudent();
                    break;
                case "5":
                    System.out.println("退出学生系统");
                    // break loop;
                    return;
                default:
                    System.out.println("输入有误,请重新输入");
                    break;
            }
        }
    }

    // 添加学生
    @Override
    public void addStudent() {
        String sid = null;
        while (true) {
            System.out.println("输入学号");
            sid = sc.next();
            boolean falg = studentServicelmpl.isExists(sid);
            if (falg) {
                System.out.println("学号已经存在");
            } else {
                break;
            }
        }
        Student student = getStudentSid(sid);
        studentServicelmpl.addStudent(student);
    }

    // 删除学生
    @Override
    public void deleteStudent() {
        System.out.println("输入学号");
        String sid = sc.next();
        boolean reuost = studentServicelmpl.isExists(sid);
        if (reuost) {
            studentServicelmpl.deleteStudent(sid);
            System.out.println("删除成功");
        } else {
            System.out.println("该学号不存在");
        }
    }

    // 修改学生
    @Override
    public void updateStudent() {
        System.out.println("输入学号");
        String sid = sc.next();
        boolean reoust = studentServicelmpl.isExists(sid);
        if (!reoust) {
            System.out.println("该学生不存在");
            return;
        }
        Student student = getStudentSid(sid);
        studentServicelmpl.updateStudent(sid, student);
        System.out.println("修改成功");
    }

    // 查看学生
    @Override
    public void findAllStudent() {
        ArrayList<Student> list = studentServicelmpl.findAllStudent();
        if (list == null || list.size() < 0) {
            System.out.println("没有学生");
            return;
        }
        System.out.println("学号\t姓名\t年龄\t生日");
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            if (stu != null) {
                System.out.println(stu);
            }
        }
    }

    // 添加 修改学生
    private Student getStudentSid(String sid) {
        System.out.println("输入姓名");
        String name = sc.next();
        System.out.println("输入年龄");
        int age = sc.nextInt();
        System.out.println("输入生日");
        String birthday = sc.next();
        return new Student(sid, name, age, birthday);
    }

}
